package com.crhms.seabow.controller.test;

import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
public class FormSubmitController {

    @RequestMapping("form")
    public String formSubmit() {
        final String filePath = "C:\\Users\\hkk\\Desktop\\上传测试用";
        final String fileName = "工作安排11.rar";
        final String url = "http://10.117.130.160:8023/API/OpinionBill/OpinionBillFeedBack";

        RestTemplate restTemplate = new RestTemplate();

        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("multipart/form-data");
        headers.setContentType(type);
        headers.add("HeFeiToken","D9433BC1578F170692BB455A4CB78860");
        //设置请求体，注意是LinkedMultiValueMap
        FileSystemResource fileSystemResource = new FileSystemResource(filePath+"/"+fileName);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();
        form.add("file", fileSystemResource);
        form.add("filename",fileName);
        form.add("userId","1002");
        form.add("remark","1002");
        form.add("opinionId","1104-201507-1-00000004");
        //用HttpEntity封装整个请求报文
        HttpEntity<MultiValueMap<String, Object>> files = new HttpEntity<>(form, headers);

        String s = restTemplate.postForObject(url, files, String.class);
        System.out.println(s);

        return "ok";
    }
}
